﻿<script lang="ts" name="planList" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormRules } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { usePlanListApi } from '/@/api/plans/planList';

//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
const planListApi = usePlanListApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
	ch: [{ required: true, message: '请选择船号！', trigger: 'blur' }],
	fd: [{ required: true, message: '请选择分段！', trigger: 'blur' }],
	className: [{ required: true, message: '请选择分类！', trigger: 'blur' }],
	planStart: [{ required: true, message: '请选择计划开工！', trigger: 'blur' }],
	planEnd: [{ required: true, message: '请选择计划完工！', trigger: 'blur' }],
});

// 页面加载时
onMounted(async () => {
	const data = (await planListApi.getDropdownData(false).then((res) => res.data.result)) ?? {};
	state.dropdownData.className = data.className ?? [];
	state.dropdownData.workAreaId = data.workAreaId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {};

	if (row.id) {
		// 编辑模式：获取详细数据
		state.ruleForm = await planListApi.detail(row.id).then((res) => res.data.result);
	} else {
		// 新增模式：设置默认值
		state.ruleForm = {
			...JSON.parse(JSON.stringify(row)),
			status: '0', // 默认状态为未完成
		};
	}

	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit('reloadTable');
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await planListApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: 'error',
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="planList-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="船号" prop="ch">
							<el-input v-model="state.ruleForm.ch" placeholder="请输入船号" maxlength="30" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分段" prop="fd">
							<el-input v-model="state.ruleForm.fd" placeholder="请输入分段" maxlength="30" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分类" prop="className">
							<el-select clearable filterable v-model="state.ruleForm.className" placeholder="请选择分类">
								<el-option v-for="(item, index) in state.dropdownData.className" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="区域" prop="workAreaId">
							<el-select clearable filterable v-model="state.ruleForm.workAreaId" placeholder="请选择区域">
								<el-option v-for="(item, index) in state.dropdownData.workAreaId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="劳务队" prop="team">
							<el-input v-model="state.ruleForm.team" placeholder="请输入劳务队" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="*计划开工" prop="planStart">
							<el-date-picker v-model="state.ruleForm.planStart" type="date" placeholder="计划开工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="*计划完工" prop="planEnd">
							<el-date-picker v-model="state.ruleForm.planEnd" type="date" placeholder="计划完工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实际开工" prop="actualStart">
							<el-date-picker v-model="state.ruleForm.actualStart" type="date" placeholder="实际开工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="实际完工" prop="actualEnd">
							<el-date-picker v-model="state.ruleForm.actualEnd" type="date" placeholder="实际完工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="128" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态" prop="status">
							<g-sys-dict v-model="state.ruleForm.status" code="FinishStatusEnum" render-as="select" placeholder="请选择状态" clearable filterable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => (state.showDialog = false)">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
